<template>
  <div>
    <a-form-model ref="form" v-bind="layout" :model="model" :rules="validatorRules" style="margin-top: 20px">
      <a-row>
        <a-col span="12">
          <a-form-model-item label="活动类型">
            <a-select
              show-search
              placeholder="请选择活动类型"
              option-filter-prop="children"
              style="width:300px"
              v-model="model.activityType"
            >
              <a-select-option v-for="item in activityTypeList" :key="item.key" :value="item.key">{{ item.value }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col span="12">
          <a-form-model-item label="活动编号" required prop="activityNo">
            <a-input placeholder="请输入活动编号" v-model="model.activityNo" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row justify="start">
        <a-form-model-item label="活动主题" required prop="topic" :label-col="{span: 4 }" :wrapperCol="{span: 20 }">
          <a-input  placeholder="请输入活动主题" v-model="model.topic" />
        </a-form-model-item>
      </a-row>
      <a-row>
        <a-col span="12">
          <a-form-model-item label="活动名称" required prop="name">
            <a-input placeholder="请输入活动名称" v-model="model.name" />
          </a-form-model-item>
        </a-col>
        <a-col span="12">
          <a-form-model-item label="联系电话" required prop="phone">
            <a-input placeholder="请输入联系电话" v-model="model.phone" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row justify="start">
        <a-form-model-item label="活动地点" required prop="place" :label-col="{span: 4 }" :wrapperCol="{span: 20 }">
          <a-textarea row="3" placeholder="请输入活动地点" v-model="model.place" />
        </a-form-model-item>
      </a-row>
      <a-row>
        <a-col span="12">
          <a-form-model-item label="活动属性" required prop="activityAttr">
            <a-radio-group v-model="model.activityAttr">
              <a-radio value="1">
                课程
              </a-radio>
              <a-radio value="0">
                活动
              </a-radio>
            </a-radio-group>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col span="12">
          <a-form-model-item label="活动日期" required prop="activityDate">
            <a-date-picker format="YYYY-MM-DD HH:mm" v-model="model.activityDate" valueFormat="YYYY-MM-DD HH:mm"
                           style="width: 230px" :disabled-date="disabledDate" />
          </a-form-model-item>
        </a-col>
        <a-col span="12">
          <a-form-model-item label="活动组织者">
            <a-input-search placeholder="请选择 活动组织者" style="width: 200px" v-model="model.organizer"
                            @search="handleAddRecipient" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col span="12">
          <a-form-model-item label="随行医生">
            <a-select
              show-search
              placeholder="请选择随行医生"
              option-filter-prop="children"
              style="width:300px"
              v-model="model.doctorId"
            >
              <a-select-option v-for="item in doctorList" :key="item.key" :value="item.key">{{ item.value }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col span="12">
          <a-form-model-item label="随行护士">
            <a-select
              show-search
              placeholder="请选择责任护士"
              option-filter-prop="children"
              style="width:300px"
              v-model="model.nurseId"
            >
              <a-select-option v-for="item in nurseList" :key="item.key" :value="item.key">{{ item.value }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>

      <a-row>
        <a-col span="12">
          <a-form-model-item label="随行护理员">
            <a-select
              show-search
              placeholder="请选择随行护理员"
              option-filter-prop="children"
              style="width:300px"
              v-model="model.carerId"
            >
              <a-select-option v-for="item in carerList" :key="item.key" :value="item.key">{{ item.value }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col span="12">
          <a-form-model-item label="随行社工">
            <a-select
              show-search
              placeholder="请选择随行社工"
              option-filter-prop="children"
              style="width:300px"
              v-model="model.socialWorkerId"
            >
              <a-select-option v-for="item in socialWorkerList" :key="item.key" :value="item.key">{{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <a-form style="max-width: 500px; margin: 40px auto 0;">
      <a-form-item :wrapperCol="{span: 19, offset: 5}">
        <a-button type="primary" @click="nextStep">提交</a-button>
      </a-form-item>
    </a-form>=
  </div>
</template>

<script>
import CommonUserExtParams from '@views/community/modules/step/CommonUserExtParams'
import pick from 'lodash.pick'
import { getAction, postAction } from '@api/manage'
import moment from 'moment'

export default {
  name: 'UserExtStep1',
  components: {
  },
  data() {
    return {
      model: {
        organizer: '',
        organizerRole: '',
        organizerId: 0,
        activityAttr: '0'
      },
      layout: {
        labelCol: { span: 8 },
        wrapperCol: { span: 14 }
      },
      confirmLoading: false,
      validatorRules: {
        topic: [{ required: true, message: '请输入活动主题!' }],
        name: [{ required: true, message: '请输入活动名称!' }],
        place: [{ required: true, message: '请输入活动地点!' }],
        organizer: [{ required: true, message: '请选择活动组织者!' }],
        phone: [{ required: true, message: '请输入活动组织者联系电话!' }],
        activityDate: [{ required: true, message: '请输入活动日期!' }],
        organizerRole: [{ required: true, message: '请选择活动组织!' }]
      },
      url: {
        listEmployee: '/store/employee/select/list',
        listActivityType: '/his/activity/type/select/list',
      },
      socialWorkerList: [],
      carerList: [],
      nurseList: [],
      doctorList: [],
      activityTypeList: []
    }
  },
  created() {
    getAction(this.url.listEmployee, { post: 1 }).then((res) => {
      this.nurseList = res.result
    })
    getAction(this.url.listEmployee, { post: 0 }).then((res) => {
      this.doctorList = res.result
    }),
    getAction(this.url.listEmployee, { 'post': '3' }).then(res => {
        this.carerList = res.result
    })
    getAction(this.url.listActivityType, { 'post': '3' }).then(res => {
      this.activityTypeList = res.result
    })
  },
  methods: {
    moment,
    //限制选择日期
    disabledDate(current) {
      // 不可以选择之前的日期,今天包括之后可以选，控制大于小于来实现选择之前的日期还是之后的日期
      return current < moment().subtract(1, 'day')
    },
    nextStep() {
      if (!new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(this.model.phone)) {
        this.$message.error('您的手机号码格式不正确')
        return
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          let that = this
          postAction('/his/activity', this.model).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('nextStep')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.loading = false
          })
        } else {
          return false
        }
      })
    },
    onChange: function(e) {
      //社工
      if (e.target.value == '1') {
        this.$refs.butlerModal.show(false)
        this.$refs.butlerModal.disableSubmit = false
      } else {
        this.$refs.personnelModal.show(false)
        this.$refs.personnelModal.disableSubmit = false
      }
    },
    handleAddRecipient() {
      if (this.model.organizerRole == '') {
        this.$message.error('请选择组织者角色')
        return
      }
      if (this.model.organizerRole == '1') {
        this.$refs.butlerModal.show(false)
        this.$refs.butlerModal.disableSubmit = false
      } else {
        this.$refs.personnelModal.show(false)
        this.$refs.personnelModal.disableSubmit = false
      }
    },
    handleSignChoice(record) {
      this.model.organizerId = record.id
      this.model.organizer = record.realname
    }
  }
}
</script>

<style scoped>

</style>